package com.roomaja.roomaja.service;

/**
 * Created by 15361 on 2018/4/29.
 */

import com.roomaja.roomaja.entity.WenKuLabelEntity;
import com.roomaja.roomaja.mapper.WenKuLabelEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.monitor.SpiderMonitor;

import java.lang.reflect.Array;
import java.util.List;

//@Component
public class WenkuSpiderRunner implements CommandLineRunner {
    @Autowired
    WenKuPageProcessor wenKuPageProcessor;
    @Autowired
    WenKuLabelEntityMapper wenKuLabelEntityMapper;

    //文库爬虫根访问url
    public static final String WENKU_SPIDER_ROOT_URL = "https://wenku.baidu.com/list";
    public static final int MAX_THREAD_URLS = 9;
    public WenkuSpiderRunner() {
    }

    /**
     * 启动文库爬虫
     * @param strings
     * @throws Exception
     */
    public void run(String... strings) throws Exception {

        List<WenKuLabelEntity> wenKuLabelEntities = wenKuLabelEntityMapper.getDataByRand(45);
        String[] spiderStrs =  new String[wenKuLabelEntities.size()];
        for (int i = 0; i<spiderStrs.length; i++) {
            spiderStrs[i] = WENKU_SPIDER_ROOT_URL + "/" + String.valueOf(wenKuLabelEntities.get(i).getLabelId());
        }

        String[] spiderStrs_1 = new String[9];
        String[] spiderStrs_2 = new String[9];
        String[] spiderStrs_3 = new String[9];
        String[] spiderStrs_4 = new String[9];
        String[] spiderStrs_5 = new String[9];
        System.arraycopy(spiderStrs, MAX_THREAD_URLS * 0, spiderStrs_1,0,MAX_THREAD_URLS);
        System.arraycopy(spiderStrs, MAX_THREAD_URLS * 1, spiderStrs_2,0,MAX_THREAD_URLS);
        System.arraycopy(spiderStrs, MAX_THREAD_URLS * 2, spiderStrs_3,0,MAX_THREAD_URLS);
        System.arraycopy(spiderStrs, MAX_THREAD_URLS * 3, spiderStrs_4,0,MAX_THREAD_URLS);
        System.arraycopy(spiderStrs, MAX_THREAD_URLS * 4, spiderStrs_5,0,MAX_THREAD_URLS);

        System.out.println("文库爬虫线程启动");
        Spider spider_1 = Spider.create(this.wenKuPageProcessor).thread(5).addUrl(spiderStrs_1);
        Spider spider_2 = Spider.create(this.wenKuPageProcessor).thread(5).addUrl(spiderStrs_2);
        Spider spider_3 = Spider.create(this.wenKuPageProcessor).thread(5).addUrl(spiderStrs_3);
        Spider spider_4 = Spider.create(this.wenKuPageProcessor).thread(5).addUrl(spiderStrs_4);
        Spider spider_5 = Spider.create(this.wenKuPageProcessor).thread(5).addUrl(spiderStrs_5);

        SpiderMonitor.instance().register(spider_1);

        spider_1.start();
        spider_2.start();
        spider_3.start();
        spider_4.start();
        spider_5.start();
    }

    @Scheduled(
            cron = "0 0 3 * * ?"
    )
    public void cronJob() {
    }
}
